//
//  SSUserOrderHeaderView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/28.
//

import UIKit

class SSUserOrderHeaderView: UIView {
    
    var date: Date = Date()
    var bgView: UIView = UIView()
    var address:SSAddress = SSAddress()
    var specses: Array<SSSpec> = []
    
    var serviceType = ""
    var selectedDate = ""
    var selectedTimeSection = ""

    @IBOutlet weak var addressLab: UILabel!
    @IBOutlet weak var serviceTimeLab: UILabel!
    @IBOutlet weak var backgroundView: UIView!
    
    @IBOutlet weak var specsLab: UILabel!
    class func headerView()->SSUserOrderHeaderView{
        return Bundle.main.loadNibNamed("SSUserOrderHeaderView", owner: nil, options: nil)?.last as! SSUserOrderHeaderView
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundView.layer.cornerRadius = 8
        backgroundView.layer.masksToBounds = true

        let tapGes = UITapGestureRecognizer(target: self, action: #selector(serviceLabel(_:)))
        serviceTimeLab.addGestureRecognizer(tapGes)
        serviceTimeLab.isUserInteractionEnabled = true

    }
                      
  @objc private func serviceLabel(_ tapGes : UITapGestureRecognizer) {
      
      pickerView()

  }
    
    func setData(address:SSAddress) {
        self.address = address
        if address.contactPhone.count == 0 {
            addressLab.text = "请填写地址"
        }else{
            addressLab.text = "\(address.houseNo)/\(address.address)"
        }
    }
    
   
    @IBAction func enterAction(_ sender: Any) {
        let btn = sender as! UIButton
        
        print("\(btn.tag)")
        if btn.tag == 1 {
            let vc = SSAddressListController.init()
            vc.userOrderController = UIViewController.current() as! SSBaseUserOrderController?
            vc.closure { finish in
                self.setData(address: finish)

            }
            UIViewController.current().navigationController?.pushViewController(vc, animated: true)
//            pickerView()
        }else if(btn.tag == 2){
//            pickerView()
            let vc =  SSOrderTimeSelectController()
            vc.selectedDate = selectedDate
            vc.selectedTimeSection = selectedTimeSection
            vc.serviceType = serviceType
            vc.dataResult { date, timeSection in
                self.selectedDate = date
                self.selectedTimeSection = timeSection
                self.serviceTimeLab.text = "\(date) \(timeSection)"
            }
    //        View Controller 被呈现后，之前的 View Controller 被移除
    //        vc.modalPresentationStyle = .fullScreen
            vc.modalPresentationStyle = .overFullScreen
            UIViewController.current().present(vc, animated: true)
        }else if(btn.tag == 3){
            let vc = SSCommonSpecController.init()
            vc.selectedItems = self.specses
            vc.numberResult { array in
                self.specses = array as! Array<SSSpec>
                var str = ""
                for(index, item) in self.specses.enumerated(){
                    if(index == 0){
                        str = item.name
                    }else{
                        str = str + "/" + item.name
                    }
                }
                self.specsLab.text = "\(str)"
            }
            
            UIViewController.current().present(vc, animated: true)
        }
    }
    
    func pickerView() {
        
        let bgView = UIView()
        bgView.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        bgView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        self.bgView = bgView
        UIViewController.current().view.addSubview(bgView)
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel(_:)))
        bgView.addGestureRecognizer(tapGes)
        
        let centerView = UIView()
        centerView.layer.cornerRadius = 8
        centerView.layer.masksToBounds = true
        centerView.frame = CGRect.init(x: (ScreenWidth - 300)/2 , y: (ScreenHeight - 160)/2, width: 300, height: 160)
        centerView.backgroundColor = UIColor.white
        bgView.addSubview(centerView)
       
        let width:CGFloat = 200
        let height:CGFloat = 60
        let NwDatePicker = UIDatePicker.init()
        NwDatePicker.frame = CGRect.init(x: (centerView.frame.width - width)/2, y: (centerView.frame.height - height)/2, width: width, height: height)
        centerView.addSubview(NwDatePicker)
        NwDatePicker.datePickerMode = .dateAndTime
        NwDatePicker.date = self.date
        NwDatePicker.addTarget(self, action: #selector(chooseDate( _:)), for:.valueChanged)
        NwDatePicker.locale = Locale.init(identifier: "zh_CN")
    }
    
    @objc private func tapedTitleLabel(_ tapGes : UITapGestureRecognizer) {
        self.bgView.removeFromSuperview()

    }
    
    /**
     选择后的时间
     */
  @objc  func chooseDate(_ datePicker:UIDatePicker) {
      self.date = datePicker.date
        let  chooseDate = datePicker.date
        let  dateFormater = DateFormatter.init()
//        dateFormater.dateFormat = "YYYY-MM-dd HH-mm-ss"
      dateFormater.dateFormat = "YYYY-MM-dd HH-mm"
        print(dateFormater.string(from: chooseDate))
      self.serviceTimeLab.text = dateFormater.string(from: chooseDate)
    }
                      
                      
    
}
    
    

